package com.ghsc.ghxc.jsb.ui.my.activity

import android.Manifest
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.copyText
import com.cn.appcore.ext.dp
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.saveBitmap
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.RxHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityInvitePageBinding
import com.king.zxing.util.CodeUtils
import com.permissionx.guolindev.PermissionX
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.my.activity
 * 类名：InvitePageActivity
 * 创建时间：2024/4/24 20:05
 * 创建人 @author： xiaolin
 * 描述 ：Todo...邀请好友
 */
class InvitePageActivity : BaseVMActivity<InvitePageViewModel, ActivityInvitePageBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_invite_page
    }

    override fun attachVMClass(): Class<InvitePageViewModel>? {
        return  InvitePageViewModel::class.java
    }

    private var inviteCode = " "
    private var inviteLinkUrl = ""
    private var disposable: Disposable? = null
    var shareImg: Bitmap? = null  //保存相册的图
    override fun onResume() {
        super.onResume()
        mViewModel.onUserInfoMethod()
    }

    override fun initView() {

        binding.topLeftView.setOnClickListener { finish() }

    }

    override fun initClickListener() {
        super.initClickListener()
        binding.tvInviteUrl.setOnClickListener {
            copyText(inviteLinkUrl)
        }
        binding.llCopyCode.setOnClickListener {
            copyText(inviteCode)
        }
        binding.btnShare.setOnClickListener {
            PermissionX.init(this@InvitePageActivity)
                .permissions(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .request { allGranted, _, _ ->
                    loge("获取到权限了吗=" + allGranted)
                    if (allGranted) {
                        binding.topLeftView.visibility = View.GONE
                        binding.btnShare.visibility = View.INVISIBLE
                        Observable.create<String> { e ->
                            var shareImgUrl =
                                saveBitmap(this, binding.inviteFriendsLayout, "custShare")
                            shareImg = BitmapFactory.decodeFile(shareImgUrl)
                            shareImgUrl?.let {
                                e.onNext(it)
                                e.onComplete()
                            }
                        }.compose(RxHelper.rxSchedulerHelper()).subscribe {
                            it?.let {
                                loge("图片已保存至相册")
                                binding.btnShare.visibility = View.VISIBLE
                                binding.topLeftView.visibility = View.VISIBLE
                                showToast("图片已保存至相册")
                            }
                        }
                    } else {
                        showToast("请开启存储权限")
                    }
                }

        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@InvitePageActivity) {
                dismissLoading()
                showToast(it)
            }
            againLoginMsg.observe(this@InvitePageActivity) {
                dismissLoading()
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onUserInfoData.observe(this@InvitePageActivity) {
                inviteCode = it.data.data.code ?: ""
                inviteLinkUrl = it.data.data.url
                binding.tvInviteCode.text = inviteCode
                createQRCode(inviteLinkUrl)

            }
        }
    }

    /**生成二维码**/
    private fun createQRCode(content: String?) {
        if (content.isNullOrBlank()) {
            showToast("生成二维码失败")
            return
        }
        disposable = Observable.just(content)
            .map { CodeUtils.createQRCode(it, 130.dp) }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                binding.ivQrCode.setImageBitmap(it)
            }
    }
    override fun onDestroy() {
        if (disposable?.isDisposed == false) {
            disposable?.dispose()
            disposable = null
        }
        super.onDestroy()

    }
}